<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: class.product_konfigurator.cart.php 485 2014-01-30 16:25:59Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 485 $
* 	zuletzt geaendert von:	$Author: $
* 	Datum:					$Date: 2014-01-30 16:25:59 +0000 (Thu, 30 Jan 2014) $
*
* 	extends by dirk.mueller (edv-mueller.com)
* 	http://www.itdotmedia.de
*
* --------------------------------------------------------------------------------------
* 	based on:
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
* 	Copyright (c) since 2011 SEO:mercari
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

class product_konfigurator_cart {
  var $konfigurator = null;
  
  function product_konfigurator_cart($konfigurator) {
    $this->konfigurator = $konfigurator;
  }

	public function add_cart(&$cart_class, &$products_id, $qty = '1', $attributes = '', $cartType, $notify = true) {
    global $db;

		if ($cart_class != null)
			$cart = &$cart_class; // restore cart
		else {
			if ($cartType == 'BASKET')
				$cart = &$_SESSION['cart'];
			else
				$cart = &$_SESSION['wish_list'];
		}

    $_POST = array_merge($_POST, (is_array($attributes)?$attributes:array()) );
    $options = array();
    foreach($this->konfigurator->get_groups() as $option_group) {
      if (isset($_POST[$option_group['id']]))
        $options[$option_group['id']] = $_POST[$option_group['id']];
    }

    if (!preg_match('/\d+{.*}/', $products_id))
   		$products_id = get_uprid($products_id, $options);

		if(!isset($_POST['product_id_edited']) && $cart->in_cart($products_id))
			$cart->update_quantity($products_id, $qty, $options);

    else {
      if (isset($_POST['product_id_edited'])) {

        if ($_POST['product_id_edited'] == $products_id) {
          //no changes
          return $false;
        }

        $new_contents = array();
  			foreach($cart->contents as $pid => $val) {
          if ($pid != $_POST['product_id_edited'])
            $new_contents[$pid] = $val;
          else
            $new_contents[$products_id] = array(
              'qty' => $val['qty'],
              'pricelist' => $val['pricelist'],
              'dimension' => ''
            );
        }

        $cart->contents = $new_contents;
        $cart->remove($_POST['product_id_edited']);
      }
  		else {
  			$cart->contents[] = array($products_id);
  			$cart->contents[$products_id] = array(
          'qty'       => $qty,
          'options'   => $options
        );
      }
    }

    if(($cart_class==null) && isset($_SESSION['customer_id'])) {
			$db->db_query("
        REPLACE INTO ".constant("TABLE_CUSTOMERS_".$cartType)."
          (customers_id, products_id, customers_basket_quantity, customers_basket_date_added)
				VALUES
          ('".$_SESSION['customer_id']."', '".$products_id."', '".$qty."', '".date('Ymd')."')");
		}	
    return true;
  }
  
  function options_price($products_key, $cart) {
    global $price;
    $option_price = $this->konfigurator->get_price($cart->contents[$products_key]['options']);
//echo_e($option_price);
    return $price->format($option_price['all']['netto'], false, $this->konfigurator->tax_id);
  }

}